package org.oneblog.data.util;


/**
 * @author Smile.Wu
 * @date 2016年4月2日  下午1:19:46
 */
public class StringFormat {
    public static Integer getInt(Object o, Integer dft) {
        if (o == null) {
            return dft;
        }
        String v = o.toString();
        return getInt(v, dft);
    }
	public static Integer getInt(String v, Integer dft) {
		if (isNumeric(v)) {
			return (int) Double.parseDouble(v);
		}
		return dft;
	}

	public static Float getFloat(String v, Float dft) {
		if (isNumeric(v)) {
			return Float.parseFloat(v);
		}
		return dft;
	}

	public static Float getFloat(Object o, Float dft) {
		if (o == null) {
			return dft;
		}
		return getFloat(o.toString(), dft);
	}

	public static Double getDbl(String v, Double dft) {
		try {
			return Double.parseDouble(v);
		} catch (Exception e) {
		}
		return dft;
	}

	public static Double getDbl(Object o, Double dft) {
        if (o == null) {
            return dft;
        }
        
		return getDbl(o.toString(), dft);
	}
	
	public static Long getLong(String v, Long dft) {
		if (isNumber(v)) {
			return Long.parseLong(v);
		}
		return dft;
	}

	public static Long getLong(Object v, Long dft) {
		if (v == null) {
			return dft;
		}
		return getLong(v.toString(), dft);
	}

	public static Boolean getBoolean(String v, Boolean dft) {
		if (StringTools.isNotBlank(v)) {
			if (v.equalsIgnoreCase("true")) {
				return true;
			}
			if (getInt(v, -1) > 0) {
				return true;
			}
		}
		return dft;
	}

	public static String getString(String s, String dft) {
		if (StringTools.isNotBlank(s)) {
			return s;
		}
		return dft;
	}

	public static Integer getIntFromBoolean(String v, Integer trueInt, Integer falseInt) {
		if (getBoolean(v, false)) {
			return trueInt;
		}
		return falseInt;
	}

	public static boolean isNumeric(String s) {
		if (!StringTools.isNotBlank(s))
			return false;
		return StaticValue.Regex.NUMERIC.matcher(s).matches();
	}

	public static boolean isNumber(String s) {
		if (!StringTools.isNotBlank(s))
			return false;
		return StaticValue.Regex.NUMBER.matcher(s).matches();
	}
}
